package com.example.designpatterns.command.concrete;

import com.example.designpatterns.command.Command;
import com.example.designpatterns.command.receiver.Light;

/**
 * LightOffCommand类 - 灯光关闭命令
 */
public class LightOffCommand implements Command {
    private Light light;
    private int previousBrightness;
    
    /**
     * 创建灯光关闭命令
     * @param light 灯光对象
     */
    public LightOffCommand(Light light) {
        this.light = light;
    }
    
    @Override
    public void execute() {
        // 保存当前亮度以便撤销
        previousBrightness = light.getBrightness();
        light.turnOff();
    }
    
    @Override
    public boolean undo() {
        if (previousBrightness > 0) {
            light.turnOn();
            light.setBrightness(previousBrightness);
            return true;
        }
        return false;
    }
    
    @Override
    public String getName() {
        return "关闭" + light.getLocation() + "的灯光";
    }
} 